home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1459.dms / var1459.adf / LowLevelGraphics / Example10.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  8KB  |  265 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: LowLevelGraphics            Tulevagen 22       */
  8. /* File:    Example10.c                 181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to use the Area Fill functions. */
  21. /* [ AreaMove(), AreaDraw() and AreaEnd(). ]                     */
  22.  
  23.  
  24. #include <intuition/intuition.h>
  25. #include <graphics/gfxbase.h>
  26. #include <graphics/gfxmacros.h>
  27.  
  28.  
  29. #define WIDTH  320 /* 320 pixels wide (low resolution)                */
  30. #define HEIGHT 200 /* 200 lines high (non interlaced NTSC display)    */ 
  31. #define DEPTH    2 /* 2 BitPlanes should be used, gives four colours. */
  32. #define COLOURS  4 /* 2^2 = 4                                         */
  33.  
  34.  
  35. #define MAX_VERTICES 100 /* 100 vertices, 5 bytes each = 500 bytes. */ 
  36. #define BUFFERT_SIZE 250 /* 500 bytes = 250 words.                  */
  37.  
  38.  
  39. struct IntuitionBase *IntuitionBase;
  40. struct GfxBase *GfxBase;
  41.  
  42.  
  43. struct View my_view;
  44. struct View *my_old_view;
  45. struct ViewPort my_view_port;
  46. struct RasInfo my_ras_info;
  47. struct BitMap my_bit_map;
  48.  
  49. struct RastPort my_rast_port;
  50. struct TmpRas  my_temp_ras;
  51. struct AreaInfo my_area_info;
  52.  
  53.  
  54. UWORD my_color_table[] =
  55. {
  56.   0x000, /* Colour 0, Black */
  57.   0xF00, /* Colour 1, Red   */
  58.   0x0F0, /* Colour 2, Green */
  59.   0x00F  /* Colour 3, Blue  */
  60. };
  61.  
  62.  
  63. /* The buffert must start on a word boundary: */
  64. UWORD buffert[ BUFFERT_SIZE ];
  65. PLANEPTR extra_space;
  66.  
  67.  
  68. void clean_up();
  69. void main();
  70.  
  71.  
  72. void main()
  73. {
  74.   UWORD *pointer;
  75.   int loop;
  76.   
  77.  
  78.   /* Open the Intuition library: */
  79.   IntuitionBase = (struct IntuitionBase *)
  80.     OpenLibrary( "intuition.library", 0 );
  81.   if( !IntuitionBase )
  82.     clean_up( "Could NOT open the Intuition library!" );
  83.  
  84.   /* Open the Graphics library: */
  85.   GfxBase = (struct GfxBase *)
  86.     OpenLibrary( "graphics.library", 0 );
  87.   if( !GfxBase )
  88.     clean_up( "Could NOT open the Graphics library!" );
  89.  
  90.  
  91.   /* Save the current View, so we can restore it later: */
  92.   my_old_view = GfxBase->ActiView;
  93.  
  94.  
  95.   /* Prepare the View structure, and give it a pointer to */
  96.   /* the first ViewPort:                                  */
  97.   InitView( &my_view );
  98.   my_view.ViewPort = &my_view_port;
  99.  
  100.  
  101.   /* Prepare the ViewPort structure, and set some important values:     */
  102.   InitVPort( &my_view_port );
  103.   my_view_port.DWidth = WIDTH;         /* Set the width.                */
  104.   my_view_port.DHeight = HEIGHT;       /* Set the height.               */
  105.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
  106.   my_view_port.Modes = NULL;           /* Low resolution.               */
  107.  
  108.  
  109.   /* Get a colour map, link it to the ViewPort, and prepare it: */
  110.   my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  111.   if( my_view_port.ColorMap == NULL )
  112.     clean_up( "Could NOT get a ColorMap!" );
  113.  
  114.   /* Get a pointer to the colour map: */
  115.   pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
  116.  
  117.   /* Set the colours: */
  118.   for( loop = 0; loop < COLOURS; loop++ )
  119.     *pointer++ = my_color_table[ loop ];
  120.  
  121.  
  122.   /* Prepare the BitMap: */
  123.   InitBitMap( &my_bit_map, DEPTH, WIDTH, HEIGHT );
  124.  
  125.   /* Allocate memory for the Raster: */ 
  126.   for( loop = 0; loop < DEPTH; loop++ )
  127.   {
  128.     my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH, HEIGHT );
  129.     if( my_bit_map.Planes[ loop ] == NULL )
  130.       clean_up( "Could NOT allocate enough memory for the raster!" );
  131.  
  132.     /* Clear the display memory with help of the Blitter: */
  133.     BltClear( my_bit_map.Planes[ loop ], RASSIZE( WIDTH, HEIGHT ), 0 );
  134.   }
  135.  
  136.   
  137.   /* Prepare the RasInfo structure: */
  138.   my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure.  */
  139.   my_ras_info.RxOffset = 0;         /* The top left corner of the Raster */
  140.   my_ras_info.RyOffset = 0;         /* should be at the top left corner  */
  141.                                     /* of the display.                   */
  142.   my_ras_info.Next = NULL;          /* Single playfield - only one       */
  143.                                     /* RasInfo structure is necessary.   */
  144.  
  145.   /* Create the display: */
  146.   MakeVPort( &my_view, &my_view_port );
  147.   MrgCop( &my_view );
  148.  
  149.  
  150.   /* Prepare the RastPort, and give it a pointer to the BitMap. */
  151.   InitRastPort( &my_rast_port );
  152.   my_rast_port.BitMap = &my_bit_map;
  153.  
  154.  
  155.  
  156.   /* 1. Get some space for the vertices and initialize the AreaInfo ptr: */
  157.   InitArea( &my_area_info, buffert, MAX_VERTICES );
  158.   my_rast_port.AreaInfo = &my_area_info;
  159.  
  160.  
  161.   /* 2. Allocate some space that is needed to build up the objects: */
  162.   extra_space = (PLANEPTR) AllocRaster( WIDTH, HEIGHT );
  163.   if( extra_space == NULL )
  164.     clean_up( "Could NOT allocate enough memory for the temp raster!" );
  165.  
  166.  
  167.   /* 3. Initialize the TmpRas structure: */
  168.   my_rast_port.TmpRas = (struct TmpRas *)
  169.     InitTmpRas( &my_temp_ras, extra_space, RASSIZE( WIDTH, HEIGHT ) );
  170.  
  171.  
  172.  
  173.   /* Show the new View: */
  174.   LoadView( &my_view );
  175.  
  176.  
  177.  
  178.   SetAPen( &my_rast_port, 1 ); /* Red   */
  179.   SetBPen( &my_rast_port, 0 ); /* Black */
  180.   SetOPen( &my_rast_port, 2 ); /* Green */
  181.   SetDrMd( &my_rast_port, JAM1 );
  182.  
  183.  
  184.  
  185.   /* New position: */
  186.   AreaMove( &my_rast_port,  10,  10 );
  187.  
  188.   /* Add the vertices: */
  189.   AreaDraw( &my_rast_port, 310,  10 );
  190.   AreaDraw( &my_rast_port, 310, 100 );
  191.   AreaDraw( &my_rast_port, 290, 100 );
  192.   AreaDraw( &my_rast_port, 290,  30 );
  193.   AreaDraw( &my_rast_port,  30,  30 );
  194.   AreaDraw( &my_rast_port,  30, 100 );
  195.   AreaDraw( &my_rast_port,  10, 100 );
  196.  
  197.   /* End this object. The last line will be set automatically in order */
  198.   /* to close the object, and the figure will be filled. The Outline   */
  199.   /* pen (green) will be used to draw a line around the whole object.  */
  200.   AreaEnd( &my_rast_port );
  201.  
  202.  
  203.   /* Turn off the outline function: */
  204.   BNDRYOFF( &my_rast_port );
  205.  
  206.  
  207.   /* New position: (This figure will not be outlined.) */
  208.   AreaMove( &my_rast_port,  10,  190 );
  209.  
  210.   /* Add the vertices: */
  211.   AreaDraw( &my_rast_port,  10, 150);
  212.   AreaDraw( &my_rast_port, 310, 190);
  213.   AreaDraw( &my_rast_port, 310, 150);
  214.  
  215.   /* End this object: */
  216.   AreaEnd( &my_rast_port );
  217.  
  218.  
  219.  
  220.   /* Wait 10 seconds: */
  221.   Delay( 50 * 10 );
  222.  
  223.  
  224.   /* Restore the old View: */
  225.   LoadView( my_old_view );
  226.  
  227.  
  228.   /* Free all allocated resources and leave. */
  229.   clean_up( "THE END" );
  230. }
  231.  
  232.  
  233. /* Returns all allocated resources: */
  234. void clean_up( message )
  235. STRPTR message;
  236. {
  237.   int loop;
  238.  
  239.   /* Deallocate memory used for the objects: */
  240.   if( extra_space )
  241.     FreeRaster( extra_space, WIDTH, HEIGHT );
  242.  
  243.   /* Free automatically allocated display structures: */
  244.   FreeVPortCopLists( &my_view_port );
  245.   FreeCprList( my_view.LOFCprList );
  246.   
  247.   /* Deallocate the display memory, BitPlane for BitPlane: */
  248.   for( loop = 0; loop < DEPTH; loop++ )
  249.     if( my_bit_map.Planes[ loop ] )
  250.       FreeRaster( my_bit_map.Planes[ loop ], WIDTH, HEIGHT );
  251.  
  252.   /* Deallocate the ColorMap: */
  253.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  254.  
  255.   /* Close the Graphics library: */
  256.   if( GfxBase ) CloseLibrary( GfxBase );
  257.  
  258.   /* Close the Intuition library: */
  259.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  260.  
  261.   /* Print the message and leave: */
  262.   printf( "%s\n", message ); 
  263.   exit();
  264. }
  265.